package com.eportal.action.background;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.domain.City;
import com.eportal.service.CityService;

import net.sf.json.JSONArray;

/**
 * 省市区查询
 * @author chencheng
 *
 */
public class CityAction extends BaseAction {

	
	private Logger log = Logger.getLogger(CityAction.class);
	
    @Autowired
    CityService cityService;

    /**
     * 获取省
     *
     * @throws IOException
     */
    public void province() throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<City> provinces = cityService.getProvices();
            map.put("code", 1);
            map.put("provinces", provinces);
        } catch (Exception e) {
            log.error(this,e);
            map.put("code", -1);
            map.put("msg", "获取数据失败");
        }
        responseBody(map);
    }

    /**
     * 根据省获取市
     *
     * @throws IOException
     */
    public void city() throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String province = getRequest().getParameter("province");
            List<City> cities = cityService.getCitys(province);
            map.put("code", 1);
            map.put("cities", cities);
        } catch (Exception e) {
            log.error(this,e);
            map.put("code", -1);
            map.put("msg", "获取数据失败");
        }
        responseBody(map);
    }


    /**
     * 根据市获取区
     *
     * @throws IOException
     */
    public void region() throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String city = getRequest().getParameter("city");
            List<City> regions = cityService.regions(city);
            map.put("code", 1);
            map.put("regions", regions);
        } catch (Exception e) {
            log.error(this,e);
            map.put("code", -1);
            map.put("msg", "获取数据失败");
        }
        responseBody(map);
    }

    public String data() {
        try {
            List<City> provinces = cityService.getProvices();
            List<Map<String, List<City>>> citys = new ArrayList<Map<String, List<City>>>();
            List<Map<String, List<City>>> regions = new ArrayList<Map<String, List<City>>>();
            for (City c : provinces) {
                List<City> cities = cityService.getCitys(c.getDm());
                Map<String, List<City>> map = new HashMap<String, List<City>>();
                map.put(c.getDm(), cities);
                citys.add(map);
                for (City city : cities) {
                    List<City> list = cityService.regions(city.getDm());
                    if (list.size() > 0) {
                        Map<String, List<City>> temp = new HashMap<String, List<City>>();
                        temp.put(c.getDm(), cities);
                        regions.add(temp);
                    }
                }
            }
            JSONArray obj1 = JSONArray.fromObject(citys);
            JSONArray obj2 = JSONArray.fromObject(regions);
            FileWriter writer = new FileWriter("E://1.json");
            writer.write(obj1.toString());
            writer.write("//n" + obj2.toString());
            writer.close();
        } catch (Exception e) {
            log.error(this,e);
        }
        return null;
    }
}
